import { Module, Logger } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { AppController } from "./app.controller";
import { MYSQL_CONFIG, REDIS_CONFIG } from "./config";
import { HttpModule } from "@nestjs/axios";
import { AppService } from "./app.service";
import { TypeOrmModule } from "@nestjs/typeorm";
import { TaskModule } from "./modules/tasks/task.module";
import { TranslateModule } from "./modules/translate/translate.module";
import { Connection } from "typeorm";
import { RedisModule } from "@liaoliaots/nestjs-redis";
import { RedisCacheModule } from "./redis/rediscache.module";

const env = process.env.env || "online";
console.log("env: ", env);
const mysqlConfig = MYSQL_CONFIG[env];
console.log("mysqlConfig: ", mysqlConfig);
const redisConfig = REDIS_CONFIG[env];

@Module({
  controllers: [AppController],
  imports: [
    TypeOrmModule.forRoot({
      type: "mysql",
      host: <string>mysqlConfig.host,
      port: <number>(<unknown>mysqlConfig.port),
      username: <string>mysqlConfig.username,
      password: <string>mysqlConfig.password,
      database: <string>mysqlConfig.database,
      entities: ["dist/**/*.entities{.ts,.js}"],
      synchronize: true,
      dropSchema: true,
    }),
    RedisModule.forRoot({
      config: {
        host: redisConfig.host,
        port: redisConfig.port,
        password: redisConfig.password,
        db: redisConfig.db,
      },
    }),
    TaskModule,
    RedisCacheModule,
    TranslateModule,
    {
      module: HttpModule,
      global: true,
    },
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: `.env.${process.env.NODE_ENV}`,
    }),
  ],
  providers: [AppService, Logger],
})
export class AppModule {
  constructor(private connection: Connection) {}
}
